package com.baboba.rummy.shuffler;

import java.util.Date;
import java.util.Random;

public class RandomExchangeShuffler<E> implements IShuffler<E> {

    @Override
    public void shuffle(E[] elements) {
            if (elements == null || elements.length==1)
                    return;
            
            int shuffleTimes = elements.length-1;
            Random random = new Random((new Date()).getTime());
            
            while(shuffleTimes>=0){
                    int left = random.nextInt(elements.length-1);
                    int right = shuffleTimes;
                    
                    while(left == right){
                            left = random.nextInt(elements.length-1);
                    }
                    
                    exchange(elements, left, right);
                    shuffleTimes--;
            }
    }

    protected void exchange(E[] elements, int left, int right){
            E element = elements[left];
            elements[left] = elements[right];
            elements[right] = element;
    }
}
